import { _decorator } from "cc";
import { Singleton } from "../decorators/Singleton";
import { StateMachine } from "./StateMachine";
const { ccclass } = _decorator;

@Singleton
@ccclass("FSMManager")
export class FSMManager {
  private _stateMachines: Map<string, StateMachine> = new Map();

  constructor() {}

  public createStateMachine(id: string): StateMachine {
    if (this._stateMachines.has(id)) {
      console.warn(`StateMachine with id ${id} already exists!`);
      return this._stateMachines.get(id);
    }

    const stateMachine = new StateMachine(id);
    this._stateMachines.set(id, stateMachine);
    return stateMachine;
  }

  public getStateMachine(id: string): StateMachine | null {
    return this._stateMachines.get(id) || null;
  }

  public removeStateMachine(id: string): void {
    this._stateMachines.delete(id);
  }

  public update(dt: number): void {
    this._stateMachines.forEach((stateMachine) => {
      stateMachine.update(dt);
    });
  }
}
